package com.umpay.utils;

/**
 * 
 * @ClassName: StringUtils  
 * @Description: string工具类 
 * @author zhangtao  
 * @date Jan 31, 2018
 */
public class StringUtils {
	
    public static final char UNDERLINE='_';  //转变的依赖字符
    public static final String DEFALULE_SP_REGEX = ",";
    
    public static String trim(Object o) {
        if(null != o) {
            return o.toString().trim();
        }
        return "";

    }

    public static boolean isEmpty(String str) {
        str = trim(str);
        if(str.equals("")) {
            return true;
        }else {
            return false;
        }
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }
    
    public static boolean isEmpty(Object object){
    	return object == null;
    }
    
    public static boolean isNotEmpty(Object object){
    	return !isEmpty(object);
    }
    /** 
     * 将驼峰转换成"_"(userId:user_id) 
     * @param param 
     * @return 
     */  
    public static String camelToUnderline(String param){    
        if (param==null||"".equals(param.trim())){    
            return "";    
        }    
        int len=param.length();    
        StringBuilder sb=new StringBuilder(len);    
        for (int i = 0; i < len; i++) {    
            char c=param.charAt(i);    
            if (Character.isUpperCase(c)){    
                sb.append(UNDERLINE);    
                sb.append(Character.toLowerCase(c));    
            }else{    
                sb.append(c);    
            }    
        }    
        return sb.toString();    
    }  
    /** 
     * 将"_"转成驼峰(user_id:userId) 
     * @param param 
     * @return 
     */  
    public static String underlineToCamel(String param){    
        if (param==null||"".equals(param.trim())){    
            return "";    
        }    
        int len=param.length();    
        StringBuilder sb=new StringBuilder(len);    
        for (int i = 0; i < len; i++) {    
            char c=param.charAt(i);    
            if (c==UNDERLINE){    
                if (++i<len){    
                    sb.append(Character.toUpperCase(param.charAt(i)));    
                }    
            }else{    
                sb.append(c);    
            }    
        }    
        return sb.toString();    
    }

    /**
     * 检查字符串数组是否包含目标值
     * @param arr
     * @param targetValue
     * @return
     */
    public static boolean contains(String[] arr, String targetValue) {
        for (String s : arr) {
            if (s.equals(targetValue))
                return true;
        }
        return false;
    }

    /**
     * 用分隔符分割字符串，并判断是否包含特定字符
     * @param arr
     * @param targetVale
     * @param regex
     * @return
     */
    public static boolean contains(String arr, String targetVale, String regex) {
        if (isEmpty(regex)) {
            String[] arrs = arr.split(DEFALULE_SP_REGEX);
            return contains(arrs, targetVale);
        } else {
            String [] arrs = arr.split(regex);
            return contains(arrs, regex);
        }
    }
    
    public static boolean containsTrue(String arr, String targetVale) {
    	if(arr.indexOf("true") > -1) {
    		return true;
    	}
    	return contains(arr, targetVale);
    }

    public static boolean contains(String arr, String targetValue) {
        return contains(arr, targetValue, "");
    }
}
